﻿using Ascend;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Media3D;

namespace Demo04_BasicAnimation
{
	/// <summary>
	/// Interaction logic for MainWindow.xaml
	/// </summary>
	public partial class MainWindow : Window
	{
		#region Lifecycle
		public MainWindow()
		{
			InitializeComponent();

			// Set data context for bindings
			DataContext = this;

			// Create our Scene3D object
			Scene = new Scene3D();

			// Setup the camera
			Scene.Camera.Position = new Point3D(6, 2, 8);

			// Load animated model and start "Idle" animation
			_sydney = Loader.Load<AnimatedSceneNode>("Models\\sydney.asc");
			_sydney.Parent = Scene.RenderRoot;
			_sydney.Play("Idle", true, true, 35);

			// Add handler for animation completions
			_sydney.AnimationCompleted += OnJumpCompleted;
			
			// Point camera at model
			Scene.LookAt(_sydney);

			// Add light to scene
			LightSceneNode light =
				LightSceneNode.Create(new DirectionalLight(Colors.White, new Vector3D(0, -1, -2)));
			light.Parent = Scene.RenderRoot;
		}
		#endregion

		#region Private Members
		private AnimatedSceneNode _sydney;
		#endregion

		#region Public Properties
		/// <summary>
		/// Gets the Scene3D that is rendered.
		/// </summary>
		public Scene3D Scene
		{
			get;
			private set;
		}

		/// <summary>
		/// Command to play "Jump" animation
		/// </summary>
		public ICommand PlayJumpCommand
		{
			get
			{
				return new InterfaceCommand(
					() =>
					{
						_sydney.Play("Jump", false, true, 50);
					},
					() => _sydney != null && _sydney.CurrentAnimation != "Jump"
				);
			}
		}
		#endregion

		#region Private Methods
		/// <summary>
		/// Re-queries button after animation is finished and starts "Idle" animation
		/// </summary>
		private void OnJumpCompleted(object sender, SceneNodeAnimationEventArgs e)
		{
			if (e.AnimationName == "Jump")
			{
				Dispatcher.Invoke(CommandManager.InvalidateRequerySuggested);

				_sydney.Play("Idle", true, true, 35);
			}
		}
		#endregion
	}
}
